# 用方向键控制方块移动
# 方向键松开, 停止移动
# 碰撞后减1分, 重新碰撞再减一分, 碰到边缘减一分
# 随机多个小方块, 方块朝着玩家行走, 走直线直到走出屏幕消失

import random
import pygame
import sys

from pygame_test.MoveRect import MoveRect
from pygame_test.pygame_09_two_rects_02 import two_rects


def move():
    pygame.init()
    winx = 300
    winy = 300
    screen = pygame.display.set_mode((winx, winy))
    fclock = pygame.time.Clock()
    fps = 10
    speedx = 5
    speedy = 5
    b = 'still'  # 大方块运动状态标记
    f = 10  # 分数
    is_in = True  # 是否在窗口内
    rect1 = MoveRect(55, 66, 20, 25, (255, 0, 255))
    little_rects = []
    i = 0
    # font = pygame.font.SysFont('SimHei', 20)
    font = pygame.font.Font(None, 20)

    while True:
        screen.fill((0, 0, 0))
        if f>0:
            text = font.render(f'score: {f}', True, (255,255,255))
        else:
            text = font.render('GAMEOVER', True, (255, 255, 255))
        screen.blit(text, (2, 2))
        i += 1
        if i % 5 == 0:
            rx = random.randint(0, winx)
            ry = random.randint(0, winy)
            # print (f'rx:{rx}, ry:{ry}')
            lr = MoveRect(rx, ry, 5, 5, (255, 255, 255))
            # 设定初始运动方向 就不再改变了
            lr.speedx, lr.speedy = two_rects(lr.get_rect(), rect1.get_rect())
            little_rects.append(lr)
        rect1.draw(screen)
        for r in little_rects:
            if not r.is_in(0, 0, winx, winy):
                little_rects.remove(r)

            r.auto_move()
            r.draw(screen)
            if rect1.is_collide(r.get_rect()) and f > 0:
                if r.is_collided == False:
                    f -= 1
                    print('collide', f, r.is_collided)
                    r.is_collided = True
                    # 碰到以后小方块就消失了
                    little_rects.remove(r)
            else:
                r.is_collided = False
        # 判断是否在屏幕内
        if not rect1.is_in(0, 0, winx, winy) and f > 0:
            if is_in:
                f -= 1
                is_in = False
                print(f)
        else:
            is_in = True
        # print(rect1.is_in(winx,winy))
        if f > 0:
            if b == 'down':
                rect1.move(0, speedy)
            elif b == 'up':
                rect1.move(0, -speedy)
            elif b == 'left':
                rect1.move(-speedx, 0)
            elif b == 'right':
                rect1.move(speedx, 0)
        else:
            print('游戏结束!')

        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if e.type == pygame.KEYDOWN:
                if e.key == pygame.K_DOWN:
                    b = 'down'
                elif e.key == pygame.K_UP:
                    b = 'up'
                elif e.key == pygame.K_LEFT:
                    b = 'left'
                elif e.key == pygame.K_RIGHT:
                    b = 'right'
            elif e.type == pygame.KEYUP:
                if e.key in [pygame.K_DOWN, pygame.K_UP, pygame.K_RIGHT, pygame.K_LEFT]:
                    b = 'still'
        pygame.display.update()
        fclock.tick(fps)


if __name__ == '__main__':
    move()
